"""This module contains numeric manipulation functions which assume that a 
given integer is being encoded in the decimal base.
"""

"""Project Euler Solutions Library

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""


import math

def get_digit_at(integer, index):    
    """Returns the digit in integer at index, [index]."""
    return int((integer % 10 ** (index + 1)) / 10 ** index)
    

def range_for_number_of_digits(number_of_digits, step = 1):
    """Generates all the numbers with [number_of_digits] digits.
    
    Optional Parameters:
        step - The number which is consecutively added to the generated
              result in order to get the next one. If not specified, this
              defaults to 1.
    
    """
    return xrange(
                 10 ** (number_of_digits - 1),
                 10 ** (number_of_digits),
                 step
                )
    
def join_integers(list_of_integers):
    """Returns all the integers in [list_of_integers] joined together, so that 
    they form one number.
    """
    #Go through all the integers, backwards, and add each one to the final
    #number, multiplying it beforehand by 10**[current length of number].
    num_of_integers = len(list_of_integers)
    number = 0
    
    current_length_of_number = 0
    
    for i in range(num_of_integers - 1, -1, -1):
        number += list_of_integers[i] * 10 ** current_length_of_number
        
        if list_of_integers[i] == 0:
            current_length_of_number += 1
        else:
            current_length_of_number += number_of_digits(list_of_integers[i])       
        
    return number

def number_of_digits(integer):
    """Returns the number of digits in [integer]."""
    
    if integer > 0:
        return int(math.log10(integer)) + 1
    else:
        return 0

def integer_to_digits(integer):
    """Returns the digits in [integer]."""
    
    integer = int(integer)
    return (integer / (10 ** i) - integer / (10 ** (i + 1)) * 10 
                for i in xrange(number_of_digits(integer) - 1, -1, -1))
        

def isone_to_nine_pandigital(n):
    """Returns true if [n] contains the digits 1 through 9 exactly once."""
    
    digits = list(integer_to_digits(n))
    return len(digits) == 9 and all(num in digits for num in range(1, 10))



